package com.gold.controller;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gold.entity.AdminUser;
import com.gold.entity.ResultMsg;
import com.gold.myexception.Myexception;
import com.gold.service.AdminService;
import com.gold.util.Myutil;

@Controller
public class AdminController {

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private AdminService as;

	@RequestMapping("getAjaxCode")
	@ResponseBody
	public Object getAjaxCode(String adminName, HttpServletRequest request) {
		log.info(adminName + "--->getAjaxCode()");
		String code = "";
		AdminUser au = as.selectAdminByAccount(adminName);
		if (au == null) {
			return new ResultMsg(false, "用户不存在");
		}
		for (int i = 0; i < 4; i++) {
			code += (char) (int) ((Math.random() * 10) + 48);
		}
		request.getSession().setAttribute("adminCode", code);
		request.getSession().setAttribute("adminLoginUser", au);
		request.getSession().setAttribute("getCodeTime", System.currentTimeMillis());
		return new ResultMsg(true, " 手机号：" + au.getUserPhone() + ",验证码：" + code);
	}

	@RequestMapping("checkAjaxCode")
	@ResponseBody
	public Object checkAjaxCode(String code, HttpServletRequest request) {
		String adminCode = (String) request.getSession().getAttribute("adminCode");
		if (adminCode == null) {
			return new ResultMsg(false, "验证码失效");
		}
		long nowTime = System.currentTimeMillis();
		long getCodeTime = (Long) request.getSession().getAttribute("getCodeTime");
		if (nowTime - getCodeTime > 30000) {
			return new ResultMsg(false, "验证码超时");
		}
		if (!adminCode.equals(code)) {
			return new ResultMsg(false, "验证码不正确");
		}
		return new ResultMsg(true, "");
	}

	@RequestMapping("adminLogin")
	public String adminLogin(String adminName, String adminPwd, String checkCode, HttpServletRequest request, ModelMap mm) {
		log.info(adminName + "--->adminLogin()");
		String adminCode = (String) request.getSession().getAttribute("adminCode");
		if (adminCode == null) {
			return "redirect:/gold/login.jsp";
		}
		long nowTime = System.currentTimeMillis();
		long getCodeTime = (Long) request.getSession().getAttribute("getCodeTime");
		if (nowTime - getCodeTime > 30000) {
			return "redirect:/gold/login.jsp";
		}
		AdminUser oldau = (AdminUser) request.getSession().getAttribute("adminLoginUser");
		AdminUser au = new AdminUser(0, adminName, adminPwd, null, null);
		try {
			as.checkCode(adminCode, checkCode);
			au = as.adminLogin(au, oldau);
		} catch (Myexception e) {
			mm.addAttribute("msg", e.getMessage());
			return "/gold/login.jsp";
		}
		request.getSession().setAttribute("adminLoginUser", au);
		return "redirect:/gold/index.jsp";
	}

	@RequestMapping("adminExitSystem")
	public String adminExitSystem(HttpServletRequest request) {
		log.info("adminExitSystem()");
		request.getSession().invalidate();
		return "redirect:/gold/login.jsp";
	}

	@RequestMapping("toDoLogJsp")
	public String toDoLogJsp(ModelMap mm, HttpServletRequest request) {
		AdminUser oldau = (AdminUser) request.getSession().getAttribute("adminLoginUser");
		if (oldau == null) {
			return "redirect:gold/index.jsp";
		}
		String logPath = "/home/soft01/upload/log/logs.log";
		String result = "";
		List<StringBuffer> sbList = new LinkedList<StringBuffer>();
		try {
			BufferedReader bf = new BufferedReader(new FileReader(logPath));
			while ((result = bf.readLine()) != null) {
				sbList.add(new StringBuffer(result));
			}
			if (bf != null) {
				bf.close();
			}
		} catch (FileNotFoundException e) {
			mm.addAttribute("msg", e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
		}
		mm.addAttribute("sbList", sbList);
		return "gold/doLog.jsp";
	}

}
